﻿using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MaterialSkin.Controls;

namespace homework11
{
    public partial class MainForm : MaterialForm
    {
        private SQLiteConnection connection;
        private string currentEnglishWord;
        private List<string> wordsList;
        private List<string> usedWords = new List<string>();
        private int currentIndex;

        public MainForm()
        {
            InitializeComponent();
            InitializeDatabase();
            LoadNextWord();
        }

        private void InitializeDatabase()
        {
            try
            {
                string databasePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "words.db");
                connection = new SQLiteConnection($"Data Source={databasePath};Version=3;");
                connection.Open();

                // 连接成功后的调试信息
                //MessageBox.Show("Database connection established successfully.", "Debug Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                // 加载所有单词到列表中
                wordsList = new List<string>();
                using (var cmd = new SQLiteCommand("SELECT english FROM word", connection))
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        wordsList.Add(reader["english"].ToString());
                    }
                }
            }
            catch (SQLiteException ex)
            {
                MessageBox.Show($"SQLite error: {ex.Message}\n{ex.StackTrace}", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}\n{ex.StackTrace}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void LoadNextWord()
        {
            if (wordsList == null || wordsList.Count == 0)
            {
                MessageBox.Show("你已经背完了所有单词");
                return;
            }

            if (currentIndex >= wordsList.Count)
            {
                DialogResult result = MessageBox.Show("你已经背完了所有单词。想要再背一遍吗？", "Completed", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    currentIndex = 0; // 重置索引
                    wordsList = wordsList.OrderBy(x => Guid.NewGuid()).ToList(); // 随机打乱列表
                }
                else
                {
                    Application.Exit(); // 关闭应用程序
                    return;
                }
            }

            string currentWord = wordsList[currentIndex];
            using (var cmd = new SQLiteCommand("SELECT english, chinese FROM word WHERE english = @english", connection))
            {
                cmd.Parameters.AddWithValue("@english", currentWord);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        currentEnglishWord = reader["english"].ToString();
                        labelChinese.Text = reader["chinese"].ToString();
                    }
                }
            }

            currentIndex++;
        }


        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            if (textBoxEnglish.Text.Trim().Equals(currentEnglishWord, StringComparison.OrdinalIgnoreCase))
            {
                MessageBox.Show("正确");
            }
            else
            {
                MessageBox.Show("错误");
            }
            LoadNextWord();
            textBoxEnglish.Clear();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            connection.Close();
        }

        private void labelChinese_Click(object sender, EventArgs e)
        {

        }
    }
}
